package com.hspedu.threaduse;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/10
 * @time 15:16
 */
public class Thread05 {
    public static void main(String[] args) {
        FutureTask<Integer> task1 = new FutureTask<>(new MyTask());
        FutureTask<Integer> task2 = new FutureTask<>(new MyTask2());
        FutureTask<Integer> task3 = new FutureTask<>(new MyTask3());
        new Thread(task1).start();
        new Thread(task2).start();
        new Thread(task3).start();
        int result = 0;
        Integer r1 = null;
        Integer r2 = null;
        Integer r3 = null;
        try {
            r1 = task1.get();
            r2 = task2.get();
            r3 = task3.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        result = r1 + r2 + r3;
        System.out.println("result = " + result);
    }
}

class MyTask implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        int result = 0;
        for (int i = 0; i < 10000; i++) {
            result += i;
        }
        return result;
    }
}


class MyTask2 implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        int result = 0;
        for (int i = 10000; i < 100000; i++) {
            result += i;
        }
        return result;
    }
}

class MyTask3 implements Callable<Integer>{
    @Override
    public Integer call() throws Exception {
        int result = 0;
        for (int i = 20000; i < 100000; i++) {
            result += i;
        }
        return result;
    }
}